package com.fdb.smcts.core.base;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fdb.smcts.core.constant.BaseConstant;
import com.fdb.smcts.core.constant.DataRuleType;
import com.fdb.smcts.core.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.dubbo.rpc.service.GenericService;

/**
 * Service提供继承类，主要提供一些公共方法
 * 
 * @author Cytus_
 * @since 2018-05-04 15:55:21
 * @version 1.0
 */
public class AbstractFrameworkService implements FrameworkService, DataOptionalAuthority {

	private static Logger logger = LoggerFactory.getLogger(AbstractFrameworkService.class);

	private final static String SEQUENCE_SERVICE_ID = "sequenceService";
//	private final static String DBSEQUENCE_SERVICE_ID = "dbSeqService";
	private final static String PUBSYSINFO_SERVICE_ID = "pubSysInfoService";

	/**
	 * 流水号生成
	 * 
	 * @param seqId
	 * @return
	 * @throws Exception
	 */
	public String getSequence(String seqId) throws Exception {
		return this.getSequence(seqId, null);
	}

	/**
	 * 流水号生成
	 * 
	 * @param seqId
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	public String getSequence(String seqId, String orgCode) throws Exception {
		return this.getSequence(seqId, orgCode, null);
	}

	/**
	 * 流水号生成
	 * 
	 * @param seqId
	 * @param orgCode
	 * @param prdCode
	 * @return
	 * @throws Exception
	 */
	public String getSequence(String seqId, String orgCode, String prdCode) throws Exception {
		GenericService gs = (GenericService) SpringContextUtils.getBean(SEQUENCE_SERVICE_ID);
		Object obj = gs.$invoke("getSequence", new String[] { "java.lang.String", "java.lang.String", "java.lang.String" },
				new Object[] { seqId, orgCode, prdCode });
		logger.info("SequenceID：" + seqId + "目前调用服务生成的流水号为:" + obj);
		return String.valueOf(obj);
	}

	/**
	 * 流水号生成
	 * 
	 * @param seqId
	 * @param orgCode
	 * @param prdCode
	 * @param legalOrgCode
	 * @param areaCode
	 * @param seqNum
	 * @return
	 * @throws Exception
	 */
	public String getSequence(String seqId, String orgCode, String prdCode, String legalOrgCode, String areaCode) throws Exception {
		GenericService gs = (GenericService) SpringContextUtils.getBean(SEQUENCE_SERVICE_ID);
		Object obj = gs.$invoke("getSequence",
				new String[] { "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String" },
				new Object[] { seqId, orgCode, prdCode, legalOrgCode, areaCode });
		logger.info("SequenceID：" + seqId + "目前调用服务生成的流水号为:" + obj);
		return String.valueOf(obj);
	}

	/**
	 * 批量生成流水号
	 * 
	 * @param seqId
	 * @param seqNum
	 * @return
	 * @throws Exception
	 */
	public List<String> getSequences(String seqId, int seqNum) throws Exception {
		return this.getSequences(seqId, null, seqNum);
	}

	/**
	 * 批量生成流水号
	 * 
	 * @param seqId
	 * @param orgCode
	 * @param seqNum
	 * @return
	 * @throws Exception
	 */
	public List<String> getSequences(String seqId, String orgCode, int seqNum) throws Exception {
		return this.getSequences(seqId, orgCode, null, seqNum);
	}

	/**
	 * 批量生成流水号
	 * 
	 * @param seqId
	 * @param orgCode
	 * @param prdCode
	 * @param legalOrgCode
	 * @param areaCode
	 * @param seqNum
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<String> getSequences(String seqId, String orgCode, String prdCode, String legalOrgCode, String areaCode, int seqNum)
			throws Exception {
		GenericService gs = (GenericService) SpringContextUtils.getBean(SEQUENCE_SERVICE_ID);
		Object obj = gs.$invoke(
				"getSequences", new String[] { "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String",
						"java.lang.String", "java.lang.Integer" },
				new Object[] { seqId, orgCode, prdCode, legalOrgCode, areaCode, Integer.valueOf(seqNum) });
		logger.info("SequenceID：" + seqId + "目前调用服务生成的流水号为:" + obj);
		return (List<String>) obj;
	}

	/**
	 * 批量生成流水号
	 * 
	 * @param seqId
	 * @param orgCode
	 * @param prdCode
	 * @param seqNum
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<String> getSequences(String seqId, String orgCode, String prdCode, int seqNum) throws Exception {
		GenericService gs = (GenericService) SpringContextUtils.getBean(SEQUENCE_SERVICE_ID);
		Object obj = gs.$invoke("getSequences",
				new String[] { "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.Integer" },
				new Object[] { seqId, orgCode, prdCode, Integer.valueOf(seqNum) });
		logger.info("SequenceID：" + seqId + "目前调用服务生成的流水号为:" + obj);
		return (List<String>) obj;
	}

	/**
	 * 获取营业日期
	 * 
	 * @param sysId
	 * @return
	 */
	public String getOpenday(String sysId) {
		GenericService gs = (GenericService) SpringContextUtils.getBean(PUBSYSINFO_SERVICE_ID);
		Object obj = gs.$invoke("getOpenday", new String[] { "java.lang.String" }, new Object[] { sysId });
		logger.info("系统营业日期为：" + obj);
		return (String) obj;
	}

	/**
	 * 获取营业日期
	 * 
	 * @param sysId
	 * @return
	 */
	public String getOpenday() {
		GenericService gs = (GenericService) SpringContextUtils.getBean(PUBSYSINFO_SERVICE_ID);
		Object obj = gs.$invoke("getOpenday", new String[] { "java.lang.String" }, new Object[] { BaseConstant.SYS_ID_1001 });
		logger.info("系统营业日期为：" + obj);
		return (String) obj;
	}

	/**
	 * 校验是否拥有操作权限
	 */
	@Override
	public boolean checkOptionalAuthority(Map<String, String> primaryKey, String dataRule) {
		DataOptionalAuthorityDao dataAuthorityDao = this.getDataAuthorityDao();
		boolean isHave = false;
		if (Objects.nonNull(dataAuthorityDao)) {
			int num = -1;
			DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRule);
			switch (dataRuleType) {
			case Owner:
				num = dataAuthorityDao.getByOwner(primaryKey);
				break;
			case CurrOrg:
				num = dataAuthorityDao.getByCurrOrg(primaryKey);
				break;
			case CurrDownOrg:
				num = dataAuthorityDao.getByCurrDownOrg(primaryKey);
				break;
			default:
				num = -1;
				break;
			}
			isHave = num > 0;
		}
		return isHave;
	}

	public DataOptionalAuthorityDao getDataAuthorityDao() {
		return null;
	}

}
